-
Notifications
You must be signed in to change notification settings - Fork 485
PoC: Protocols for typing of Gramps objects #2128
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
PoC: Protocols for typing of Gramps objects #2128
Conversation
|
|
||
| @runtime_checkable | ||
| class GrampsTypeLike(Protocol): | ||
| """Protocol for GrampsType-like objects (NameType, AttributeType, etc.).""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If protocols are used, they should be the 'primary' types. In other words, concrete types should refer to the protocols, and not the other way around, so a GrampsType is defined as a GrampsTypeLike, but a GrampsTypeLike is not defined as a GrampsType-like.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't agree actually - unless I'm misunderstanding what you mean. The whole point of protocols is that they can be used without inheritance. For instance, DataDict cannot inherit from PersonLike because not every DataDict has a surname_list (it might be a family or an event etc.).
It's like with database proxies in Gramps that sometimes don't inherit from DbGeneric but just share a subset of its methods.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If protocols are used like interfaces (as in the abstract programming concepts), such interfaces should not reference concrete types. For example, a 'person-like' interface is not a 'person' (concrete type). Instead, it's the other way around: a 'person' concrete type is a 'person-like' interface. That way other code can type on the interface (the protocol, in Python terminology), instead of any concrete type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree. Anything in the code or in what I said that goes against that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just the docstrings, I believe. I wasn't quite clear about that
In essence, with my limited knowledge of Gramps internals, this looks like a good improvement. Are there plans to introduce additional concrete implementations in the future?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd be happy to complete it for all the other primary types so we can make use of it in other PRs if the feedback is positive.
|
This looks like a viable path forward to me. Thanks for this! |
Typing of functions/methods involving Gramps objects has become significantly more complicated in Gramps 6.0 due to the introduction of
DataDict.As was discussed in various places, e.g. here, protocols seem to be the right way to address this.
This PR contains a proof of concept, adding protocols for
all primarythePersonprimary type and some secondary Gramps types. The code at the bottom checks that the instances are compatible with the protocols (at list w.r.t. existence of methods).Related: #2018, #1934, #1919, #2010.
CC @dsblank, @stevenyoungs, @bartfeenstra